package com.ruoyi.common.utils;

import org.apache.commons.lang3.StringUtils;
import org.htmlparser.Node;
import org.htmlparser.lexer.Lexer;
import org.htmlparser.nodes.TextNode;
import org.htmlparser.util.ParserException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 字符串工具类
 *
 * @author liufang
 */
public abstract class Strings {
    private static Logger logger = LoggerFactory.getLogger(Strings.class);

    /**
     * 字符串截断。编码大于127的字符作为占两个位置，否则占一个位置。
     *
     * @param text
     * @param length
     * @param append
     * @return
     */
    public static String substring(String text, int length, String append) {
        if (StringUtils.isBlank(text) || text.length() < length) {
            return text;
        }
        int num = 0, i = 0, len = text.length();
        StringBuilder sb = new StringBuilder();
        for (; i < len; i++) {
            char c = text.charAt(i);
            if (c > 127) {
                num += 2;
            } else {
                num++;
            }
            if (num <= length * 2) {
                sb.append(c);
            }
            if (num >= length * 2) {
                break;
            }
        }
        if (i + 1 < len && StringUtils.isNotBlank(append)) {
            if (text.charAt(i) > 127) {
                sb.setLength(sb.length() - 1);
            } else {
                sb.setLength(sb.length() - 2);
            }
            sb.append(append);
        }
        return sb.toString();
    }

    public static String substring(String text, int length) {
        return substring(text, length, null);
    }

    public static String urlEncode(String s) {
        if (StringUtils.isBlank(s)) {
            return s;
        }
        try {
            return URLEncoder.encode(s, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            // never
            throw new RuntimeException(e);
        }

    }

    public static String urlDecode(String s) {
        if (StringUtils.isBlank(s)) {
            return s;
        }
        try {
            return URLDecoder.decode(s, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            // never
            throw new RuntimeException(e);
        }
    }

    public static void replace(StringBuilder sb, String searchString,
                               String replacement) {
        int start = 0;
        int end = sb.indexOf(searchString, start);
        if (end == -1) {
            return;
        }
        int searchLength = searchString.length();
        int replaceLength = replacement.length();
        while (end != -1) {
            sb.replace(end, end + searchLength, replacement);
            start = end + replaceLength;
            end = sb.indexOf(searchString, start);
        }
    }

    /**
     * 将字符串按行风格，支持windows(\r\n)、linux(\n)和(\r)格式换行。
     *
     * @param s
     * @return
     */
    public static String[] splitLines(String s) {
        if (s == null || s == "") {
            return null;
        }
        List<String> back = new ArrayList<>();
        String[] https = s.split("http");
        if (https == null || https.length == 0) {
            return null;
        }

        for (String str : https) {
            if (str.length() > 5) {
                back.add("http" + str.trim());
            }
        }

        return back.toArray(new String[back.size()]);

//		return StringUtils.split(s, "\r\n");
    }

    /**
     * 将字符串按行风格，支持windows(\r\n)、linux(\n)和(\r)格式换行。
     *
     * @param s
     * @return
     */
    public static String[] splitLines_old(String s) {
        return StringUtils.split(s, "\r\n");
    }


    /**
     * 将换行符替换成\n
     *
     * @return
     */
    public static String replaceNewline(String s) {
//        s = StringUtils.replace(s, "\r\n", "\n");
//        s = StringUtils.replaceChars(s, '\r', '\n');

        s = StringUtils.replace(s, "\n", "");
        s = StringUtils.replace(s, "\r", "");
        return s;
    }

    /**
     * 过滤Pattern匹配的字符串，并保留group数据。
     *
     * @param input
     * @param p
     * @return
     */
    public static String filter(String input, Pattern p) {
        Matcher m = p.matcher(input);
        int start = 0, end;
        StringBuilder sb = new StringBuilder();
        while (m.find()) {
            end = m.start();
            sb.append(input.subSequence(start, end));
            for (int i = 1, len = m.groupCount(); i <= len; i++) {
                sb.append(input.subSequence(m.start(i), m.end(i)));
            }
            start = m.end();
        }
        end = input.length();
        sb.append(input.subSequence(start, end));
        return sb.toString();
    }

    public static String getTextFromHtml(String html, int length) {
        if (StringUtils.isBlank(html)) {
            return html;
        }
        if (length <= 0) {
            length = Integer.MAX_VALUE;
        }
        StringBuilder buff = new StringBuilder((int) (html.length() * 0.7));
        Lexer lexer = new Lexer(html);
        Node node;
        try {
            while ((node = lexer.nextNode()) != null && buff.length() < length) {
                if (node instanceof TextNode) {
                    buff.append(node.getText());
                }
            }
        } catch (ParserException e) {
            logger.error("parse html exception", e);
        }
        if (buff.length() > length) {
            buff.setLength(length);
        }
        return buff.toString();
    }

    public static String getTextFromHtml(String html) {
        return getTextFromHtml(html, Integer.MAX_VALUE);
    }

    /**
     * 字符串拼接。el里无法拼接字符串，提供fnx函数用于拼接字符串。
     *
     * @param s1
     * @param s2
     * @return
     */
    public static String concat(String s1, String s2) {
        return s1 + s2;
    }

    /**
     * 字符串拼接。el里无法拼接字符串，提供fnx函数用于拼接字符串。
     *
     * @param s1
     * @param s2
     * @param s3
     * @return
     */
    public static String concat3(String s1, String s2, String s3) {
        return s1 + s2 + s3;
    }

    /**
     * 字符串拼接。el里无法拼接字符串，提供fnx函数用于拼接字符串。
     *
     * @param s1
     * @param s2
     * @param s3
     * @param s4
     * @return
     */
    public static String concat4(String s1, String s2, String s3, String s4) {
        return s1 + s2 + s3 + s4;
    }

    // public static String getKeywords(String s, boolean useSmart) {
    // if (StringUtils.isBlank(s)) {
    // return "";
    // }
    // StringReader reader = new StringReader(s);
    // IKSegmenter iks = new IKSegmenter(reader, useSmart);
    // StringBuilder buff = new StringBuilder();
    // try {
    // Lexeme lexeme;
    // while ((lexeme = iks.next()) != null) {
    // buff.append(lexeme.getLexemeText()).append(',');
    // }
    // } catch (IOException e) {
    // logger.warn("StringReader error!", e);
    // }
    // if (buff.length() > 0) {
    // buff.setLength(buff.length() - 1);
    // }
    // return buff.toString();
    // }
    //
    // public static String getKeywords(String s) {
    // return getKeywords(s, true);
    // }

    public static boolean isInteger(Object a_sort) {
        if (a_sort instanceof Integer) {
            return true;
        }
        try {
            if (Integer.parseInt(a_sort.toString()) > 0) {
                return true;
            }
        } catch (Exception e) {
            return false;
        }
        return false;
    }

    public static boolean isISBN(String isbn) {
        try {
            String frontStr = isbn.substring(0, isbn.length() - 1);
            String backStr = isbn.substring(isbn.length() - 1);
            boolean isNum = frontStr.matches("[0-9]+");
            if (!isNum || !(frontStr.length() == 9 || frontStr.length() == 12)) {
                return false;
            }
            char[] tmp = frontStr.toCharArray();
            int sum = 0;
            int count = 10;
            if (frontStr.length() == 9) {//验证10位的ISBN
                for (int i = 0; i < 9; i++) {

                    int dd = Integer.parseInt(tmp[i] + "");

                    sum = sum + count * dd;

                    count--;

                }
                int n = 11 - sum % 11;
                String s = "";
                if (n == 11) {
                    s = "0";
                } else if (n == 10) {
                    s = "x";
                } else {
                    s = "" + n;
                }

                if (backStr.toLowerCase().equals(s)) {
                    return true;
                } else {
                    return false;
                }
            } else if (frontStr.length() == 12) {//验证13位的ISBN
                String str = isbn.substring(0, 3);
                if (!(str.equals("979") || str.equals("978"))) {
                    return false;
                }
                for (int i = 0; i < 12; i++) {
                    int dd = Integer.parseInt(tmp[i] + "");
                    if (i % 2 == 0) {
                        sum = sum + 1 * dd;
                    } else {
                        sum = sum + 3 * dd;
                    }
                }
                String s = "" + (10 - sum % 10);

                if (backStr.equals(s)) {
                    return true;
                } else {
                    return false;
                }
            } else {
                return false;
            }
        } catch (Exception e) {
            return false;
        }

    }

}
